package fr.istic.linkedcv.client.presentation.views.impl;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

import fr.istic.linkedcv.client.CVCli;
import fr.istic.linkedcv.client.presentation.Ui;
import fr.istic.linkedcv.client.presentation.views.View;
import fr.istic.linkedcv.client.services.CVService;
import fr.istic.linkedcv.client.services.CVServiceAsync;

public class CVForm extends View {

	private Grid grid = new Grid(11, 1);
	private Label objectifLabel = new Label("Objectifs");
	private TextArea objectif = new TextArea();
	private Label etudesLabel = new Label("Études et diplômes");
	private TextArea etudes = new TextArea();
	private Label experience_proLabel = new Label(
			"Expériences professionnelles");
	private TextArea experience_pro = new TextArea();
	private Label competencesLabel = new Label("Compétences");
	private TextArea competences = new TextArea();
	private Label diversLabel = new Label("Divers");
	private TextArea divers = new TextArea();
	Button submitBtn = new Button("Enregistrer");
	private final CVServiceAsync cvService = GWT.create(CVService.class);
	protected boolean CvExist;

	public CVForm(Ui ui) {
		super(ui);
		
	}

	@Override
	public Widget getWidget() {
		// TODO Auto-generated method stub
		return grid;
	}

	public void build() {
		submitHandler();
		initWidget();
		

	}

	private void buildForm() {
		grid.setWidget(0, 0, new HTML("<h1>" + objectifLabel + "</h1>"));
		grid.setWidget(1, 0, objectif);
		grid.setWidget(2, 0, new HTML("<h1>" + etudesLabel + "</h1>"));
		grid.setWidget(3, 0, etudes);
		grid.setWidget(4, 0, new HTML("<h1>" + experience_proLabel + "</h1>"));
		grid.setWidget(5, 0, experience_pro);
		grid.setWidget(6, 0, new HTML("<h1>" + competencesLabel + "</h1>"));
		grid.setWidget(7, 0, competences);
		grid.setWidget(8, 0, new HTML("<h1>" + diversLabel + "</h1>"));
		grid.setWidget(9, 0, divers);
		grid.setWidget(10, 0, submitBtn);
	}

	private void initWidget() {
		cvService.getCV(ui.getControle().getPersonneCli().getLogin(), new AsyncCallback<CVCli>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(CVCli result) {
				if(result != null){
					objectif.setText(setWidgetText(result.getObjectif()));
					etudes.setText(setWidgetText(result.getEducation()));
					experience_pro.setText(setWidgetText(result.getExperience_pro()));
					competences.setText(setWidgetText(result.getCompetences()));
					divers.setText(setWidgetText(result.getDivers()));
					submitBtn.setText("Mettre à jour");
					CvExist = true;
				}
				buildForm();
				
			}
			
		});
		
	}

	protected String setWidgetText(String text) {
		return (text!=null)?text:"";
		
	}

	private void submitHandler() {
		submitBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (objectif.getValue().isEmpty()) {
					Window.alert("Le champ objectif doit être saisi.");
				} else if (etudes.getValue().isEmpty()) {
					Window.alert("Le champ password doit être saisi.");
				} else if (experience_pro.getValue().isEmpty()) {
					Window.alert("Le champ password doit être saisi.");
				} else if (competences.getValue().isEmpty()) {
					Window.alert("Le champ password doit être saisi.");
				} else if (divers.getValue().isEmpty()) {
					Window.alert("Le champ password doit être saisi.");
				} else{
					if(CvExist){
						updateCv();
					}else{
						createCv();
					}
				}

			}
		});

	}

	protected void createCv() {
		Map<String, String> cvMap = createCvMap();

		cvService.createCV(cvMap, new AsyncCallback<Boolean>() {

			@Override
			public void onSuccess(Boolean result) {
				ui.addHistoryItem("my_cv");
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Échec de la création du cv");

			}
		});

	}
	
	protected void updateCv() {
		Map<String, String> cvMap = createCvMap();

		cvService.updateCV(cvMap, new AsyncCallback<Boolean>() {

			@Override
			public void onSuccess(Boolean result) {
				Window.alert("La mise à jour du cv a été correctement effectué");
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Échec de la mise à jour du cv");

			}
		});

	}

	private Map<String, String> createCvMap() {
		Map<String, String> cvMap = new HashMap<String, String>();
		cvMap.put("objectif", objectif.getValue());
		cvMap.put("etudes", etudes.getValue());
		cvMap.put("experience_pro", experience_pro.getValue());
		cvMap.put("competences", competences.getValue());
		cvMap.put("divers", divers.getValue());
		return cvMap;
	}

}
